#!/bin/bash

# 该脚本只适用于nmcli,
# 改脚本只能实现单网卡修改ip地址

# 获取活动状态的ncmli配置名
## 有bug,如果配置名是默认的`Wired connection 1` 或者有空格会无法显示全
# CON_NAME=$(nmcli con show --active | awk 'END{print $1F}')
# 删除旧的con-name
# nmcli c delete "$CON_NAME"

# 删除statc这个配置
nmcli c delete static

# 获取活动的网卡名
## 打印输出的最后一列最后一行
IFACE=$(nmcli con show --active | awk 'END{print $NF}')

# 打印活动的网卡名
echo "网卡名是$IFACE"

# 打印设置的ip
echo "设置为ip为:$1"

# 配置静态ip,配置名为static
# nmcli c mod "$CON_NAME" ip4 "192.168.123.$1/24"
# nmcli c add con-name "$CON_NAME" ifname "$IFACE" type ethernet ip4 "192.168.123.$1/24" gw4 192.168.123.2 ipv4.dns 192.168.123.2 ipv4.method manual autoconnect yes
nmcli c add con-name static ifname "$IFACE" type ethernet ip4 "192.168.123.$1/24" gw4 192.168.123.2 ipv4.dns 192.168.123.2 ipv4.method manual autoconnect yes

# 重新读取配置
nmcli c reload

# 关闭配置&&激活配置
nmcli c down "$CON_NAME"

# 启用static配置
nmcli c up static

# 显示IP地址
ip a show "$IFACE"
